<?php
namespace app\blog\controller;
use app\blog\model\UserModel;
use cmf\controller\AdminBaseController;
use think\Db;

class ShopTypeController extends AdminBaseController
{
    //店铺类型列表
    
    public function index(){
       //分页、搜索      
       $where=array();
       $params=$this->request->param();
       if (isset($params['type_name'])) {
           $where['type_name'] = ['like', "%{$params['type_name']}%"];
       }
       $id = cmf_get_current_admin_id();
       if($id != 1){
           $rest = Db::name('user')->where(['id'=>$id])->find();
           $where['st.age_id'] = $rest['user_guid'];
       }
       $list=Db::name('shop_type')->alias('st')
                                        ->join('__AGENCY__ a','st.age_id=a.user_id')
                                        ->where($where)
                                        ->field('st.*,a.brand_name')
                                        ->paginate(15);
       $list->appends($params);
       $page = $list->render();
       $this->assign("page", $page);
       $this->assign('list',$list);
       return $this->fetch();
    }
    //新增店铺类型
    public function add(){
        if($this->request->param()){
            $res  = $this->request->param();
            if(empty($res['type_name'])){
                $this->error('类别名称必填');exit;
            }
            $checkinfo=Db::name('ShopType')->where(array('type_name'=>trim($res['type_name'])))->find();
            if($checkinfo){
                $this->error('类别名称已存在');exit;
            }
            $data=array(
                'st_guid'=>guid(),
                'type_name'=>trim(strip_tags($res['type_name'])),
                'age_id'=>$res['age_id']
            );
            $result=Db::name('ShopType')->insert($data);
            if($result != false){
                $this->success('新增成功', 'ShopType/index');
            }else{
                $this->error('新增失败');
            }
        }else{
            $id = cmf_get_current_admin_id();
            $this->assign('id',$id);//判断是否是admin 如果显示所有代理，否只显示登录代理
            $u = new UserModel();
            $ret = $u->getAll();
            $this->assign('ret',$ret);
            $ret1 = $u->getUsermsgs($id);
            $this->assign('ret1',$ret1);
            return $this->fetch();
        }
    }
    //编辑店铺类型
    public function edit(){
        $res  = $this->request->param();
        if($this->request->isPost()){
             $checkinfo=Db::name('ShopType')->where(array('type_name'=>trim($res['type_name'])))->find();
             if($checkinfo){
                 $this->error('类别名称已存在');exit;
             }
            $data=array(
                'type_name'=>$res['type_name'],
                'age_id'=>$res['age_id']
            );
            $result=Db::name('shop_type')->where(array('st_guid'=>$res['id']))->update($data);
            if($result != false){
                $this->success('编辑成功',"ShopType/index");
            }else{
                $this->error('编辑失败');
            }
        }else{
            $info=Db::name('shop_type')->where(['st_guid'=>$res['id']])->find();
            //这里应该有个代理列表,
            //$daili_list=......
            $this->assign('info',$info);

            $id = cmf_get_current_admin_id();
            $this->assign('id',$id);//判断是否是admin 如果显示所有代理，否只显示登录代理
            $u = new UserModel();
            $ret = $u->getAll();
            $this->assign('ret',$ret);
            $ret1 = $u->getUsermsgs($id);
            $this->assign('ret1',$ret1);
            return $this->fetch();
        }
    }
    //删除店铺类型
    public function delete(){
        //判断可以删除的条件
        $id  = $this->request->param('id');
        $check=Db::name('shop')->where(array('stype_id'=>$id))->find();
        if($check){
            $this->error('类别下有数据，不可删除');
        }else{
            $status = Db::name('shop_type')->where(['st_guid'=>$id])->delete();
            if($status){
                $this->success('删除成功！');
            }else{
                $this->error("删除失败！");
            }
        }
    }    
}